package com.gzdh.dishrecognition.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.model.BluetoothModel;

import java.util.ArrayList;
import java.util.List;

public class SysBluetoothAdapter extends RecyclerView.Adapter<SysBluetoothAdapter.ViewHolder> {
    private Context mcontext;

    private OnUserClick onUserClick;

    public interface OnUserClick {
        void userClick(BluetoothModel bluetoothModel);
    }

    public void setOnUserClick(OnUserClick onUserClick) {
        this.onUserClick = onUserClick;
    }

    public SysBluetoothAdapter(Context context) {
        mcontext = context;

    }

    private List<BluetoothModel> bluetoothModels = new ArrayList<>();

    public void clear() {
        bluetoothModels = new ArrayList<>();
        notifyDataSetChanged();
    }

    public void addBluetooth(BluetoothModel bluetoothModel) {
        BluetoothModel bluetoothModel1 = bluetoothModels.stream().filter(bluetoothModel2 -> bluetoothModel.getAddress().equals(bluetoothModel2.getAddress()))
                .findFirst().orElse(null);
        if (bluetoothModel1 == null) {
            bluetoothModels.add(bluetoothModel);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bluetooth, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.blueNameTv.setText(bluetoothModels.get(position).getName());
        holder.blueAddrTv.setText(bluetoothModels.get(position).getAddress());
        if (bluetoothModels.get(position).getSelect() > 0) {
            holder.settingBut.setVisibility(View.VISIBLE);
        }
        holder.mainLinear.setOnClickListener(view -> {
            if (bluetoothModels.get(position).getSelect() == 0 && onUserClick != null) {
                onUserClick.userClick(bluetoothModels.get(position));
            }
        });

    }


    @Override
    public int getItemCount() {
        return bluetoothModels.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView blueNameTv;

        TextView blueAddrTv;

        RelativeLayout settingBut;

        LinearLayout mainLinear;

        ViewHolder(View item) {
            super(item);
            blueNameTv = item.findViewById(R.id.blueNameTv);
            blueAddrTv = item.findViewById(R.id.blueAddrTv);
            settingBut = item.findViewById(R.id.setting_but);
            mainLinear = item.findViewById(R.id.mainLinear);
        }

    }

}
